package com.yc.boot.train.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.common.model.base.BasePageQuery;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.sys.business.SysUserBusiness;
import com.yc.boot.sys.model.UserInfo;
import com.yc.boot.train.entity.TrainCourse;
import com.yc.boot.train.model.find.CourseCount;
import com.yc.boot.train.model.find.CourseManage;
import com.yc.boot.train.model.find.IPageCourseManage;
import com.yc.boot.train.model.save.SaveCourseManage;
import com.yc.boot.train.model.save.SaveGradeManage;
import com.yc.boot.train.model.update.UpdateCourse;
import com.yc.boot.train.service.CourseCountService;
import com.yc.boot.train.service.TrainCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.yc.boot.common.constant.BaseConstant.ZERO;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/01/10/13:47
 * @Description:
 */
@Slf4j
@Service
public class CourseManageBussiness {
    @Resource
    private TrainCourseService trainCourseService;
    @Resource
    private CourseCountService courseCountService;

    @Resource
    private SysUserBusiness sysUserBusiness;
    /*
     * @Description 根据当前登录id分页查询课程表信息
     * @Param
     * @return
     **/
    public IPage<TrainCourse> queryCourseById(IPageCourseManage param){
        UserInfo user = sysUserBusiness.getUserInfo();
//        UserInfo user =new UserInfo();
//        user.setId(5L);
        IPage<TrainCourse> courseMessage =trainCourseService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<TrainCourse>().lambda()
                        .eq(ObjectUtil.isNotEmpty(user.getId()),TrainCourse::getCourseBelong,user.getId())
                        .like(ObjectUtil.isNotEmpty(param.getCourseName()),TrainCourse::getCourseName,param.getCourseName())
        );
        return courseMessage;
    }

    /*
     * @Description  添加课程信息
     * @Param
     * @return
     **/

    public Outcome saveCourseMessage(SaveCourseManage param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login=new UserInfo();
//        login.setId(5L);
        if(ObjectUtil.isNotEmpty(param.getCourseName())&&trainCourseService.count(new QueryWrapper<TrainCourse>()
        .lambda().eq(TrainCourse::getCourseName,param.getCourseName())
        )>ZERO){
            return Outcome.failure("添加失败！当前课程信息已登记！");
        }
        TrainCourse courseMessage =new TrainCourse();
        courseMessage.setCourseName(param.getCourseName());
        courseMessage.setCourseIntro(param.getCourseIntro());
        courseMessage.setCourseContent(param.getCourseContent());
        courseMessage.setCourseTime(param.getCourseTime());
        courseMessage.setCourseDuration(param.getCourseDuration());
        courseMessage.setTrainForm(param.getTrainForm());
        courseMessage.setTrainNumber(param.getTrainNumber());
        courseMessage.setCourseBelong(login.getId());
        courseMessage.setCreateBy(login.getId());
        Boolean save =trainCourseService.save(courseMessage);
        return Outcome.status(save);
    }

    /*
     * @Description  修改课程信息
     * @Param
     * @return
     **/
    public Outcome updateCourseById(UpdateCourse param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login=new UserInfo();
//        login.setId(5L);
        TrainCourse course = Convert.convert(TrainCourse.class,param);
        course.setCourseBelong(login.getId());
        course.setUpdateBy(login.getId());
        Boolean update =trainCourseService.updateById(course);
        return Outcome.status(update);
    }

/*
 * @Description 删除课程信息
 * @Param
 * @return
 **/
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> id) {
        UserInfo userInfo = sysUserBusiness.getUserInfo();
//        UserInfo userInfo =new UserInfo();
//        userInfo.setId(5L);
        Boolean updateBatchById =trainCourseService.updateBatchById(Convert.toList(TrainCourse.class,
                id.stream().map(i ->new TrainCourse().setUpdateBy(userInfo.getId()).setId(i)).collect(Collectors.toList()))
        );
        Boolean removeByids =trainCourseService.removeByIds(id);
        return updateBatchById&&removeByids;
    }

    /*
     * @Description  统计每门课程的选课人数
     * @Param
     * @return
     **/

    public List<CourseCount> queryCourseCount(){
        List<CourseCount> courseCount= courseCountService.queryCourseCount(new QueryWrapper<CourseCount>().lambda());
        return courseCount;
    }
}
